<?php

namespace App\Jobs;

use App\Models\Posts\WxPostsVideo;
use App\Wen\Utils\GatewayUtils;
use App\Wen\Utils\ImageUtils;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class VideoSnapshotJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    protected $video_id = 0;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($video_id)
    {
        //
        $this->video_id = $video_id;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        //
        if(_empty_($this->video_id)){
            return;
        }

        $post_video = WxPostsVideo::where([
            ['id', '=', $this->video_id],
            ['posts_video_state', '=', 0]
        ])->first();
        if($post_video && _empty_($post_video->video_thumb_url)){
            $video_url = $post_video['video_url'];
            $res = ImageUtils::upload_video_screenshot($video_url);
            if($res && $res['url']){
                $post_video->video_thumb_url = $res['url'];
                $post_video->save();
                GatewayUtils::success(GatewayUtils::uid2client_id($post_video->user_id), 7);
            }else{
                _logger_(__file__, __line__, $res);
            }
        }
    }
}
